package club.hicode.daydayup.netty.demo.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.junit.Test;

/**
 * 服务端 Main
 *
 * @author Liu Chunfu
 * @date 2018-06-14 下午2:50
 **/
public class ServerMain {

    @Test
    public void start1() throws InterruptedException {

        //NettyRuntime.availableProcessors()
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup work = new NioEventLoopGroup();

        System.out.println(((NioEventLoopGroup) boss).executorCount());
        System.out.println(((NioEventLoopGroup) work).executorCount());
        ServerBootstrap bootstrap = new ServerBootstrap().group(boss, work)
                .channel(NioServerSocketChannel.class)
                .handler(new MyParentChannelInitializer())
                .childHandler(new MyChannelInitializer())
                .option(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

        ChannelFuture future1 = bootstrap.bind(23790).sync();
        System.out.println("绑定端口：23790");
        ChannelFuture future2 = bootstrap.bind(23791).sync();
        System.out.println("绑定端口：23791");

        ChannelFuture sync = future1.channel().closeFuture().sync();

    }

}
